package com.tencent.wxcloudrun.service;

import com.alibaba.fastjson2.JSON;
import com.tencent.wxcloudrun.dao.GoodsMapper;
import com.tencent.wxcloudrun.model.CateGoods;
import com.tencent.wxcloudrun.model.Goods;
import com.tencent.wxcloudrun.model.GoodsSpecs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: GoodsService
 * Package: com.tencent.wxcloudrun.service
 * Description:
 *
 * @Date: 2023/1/23 2:04 PM
 * @Author: wht
 */
@Slf4j
@Service
public class GoodsService {
    @Autowired
    GoodsMapper goodsMapper;

//    private void toJson(Goods goods) {
//        goods.setSpecs_list_json(JSON.toJSONString(goods.getSpecs_list()));
//        goods.setCompose_list_json(JSON.toJSONString(goods.getCompose_list()));
//    }


    public void addOneGoods(Goods goods) {
//        this.toJson(goods);
//        System.out.println(444);
        goodsMapper.addOneGoods(goods);
    }

    public void updateOneGoodsById(Goods goods) {
//        this.toJson(goods);
        goodsMapper.updateOneGoodsById(goods);
    }

    public List<Goods> getGoodsList(Goods goods) {
        List<Goods> goodsList = goodsMapper.getGoodsList(goods);
        for (Goods goods1 : goodsList) {
            goods1.setSpecs_list(JSON.parseArray(goods1.getSpecs_list_json(), GoodsSpecs.class));
            goods1.setCompose_list(JSON.parseArray(goods1.getCompose_list_json(), CateGoods.class));
        }
        return goodsList;
    }

}
